package com.gitee.wsl.mathematics.vector.ext

import com.gitee.wsl.mathematics.vector.vec2.MutableVector2
import com.gitee.wsl.mathematics.vector.vec2.Vector2
import com.gitee.wsl.mathematics.vector.vec3.MutableVector3
import com.gitee.wsl.mathematics.vector.vec3.Vector3
import com.gitee.wsl.mathematics.vector.vec4.MutableVector4
import com.gitee.wsl.mathematics.vector.vec4.Vector4

fun<T:Number> Iterable<Vector2<T, *>>.averageTo(res: MutableVector2<T, *>)  {
    var x = res.zero
    var y = res.zero
    var count = 0
    res.run {
        for (v in this@averageTo) {
            x += v.x
            y += v.y
            count++
        }
        res.set(x/count, y/count)
    }
}

fun<T:Number> Iterable<Vector3<T, *>>.averageTo(res: MutableVector3<T, *>)  {
    var x = res.zero
    var y = res.zero
    var z = res.zero
    var count = 0
    res.run {
        for (v in this@averageTo) {
            x += v.x
            y += v.y
            z += v.z
            count++
        }
        res.set(x/count, y/count, z / count)
    }
}

fun<T:Number> Iterable<Vector4<T, *>>.averageTo(res: MutableVector4<T, *>)  {
    var x = res.zero
    var y = res.zero
    var z = res.zero
    var w = res.zero
    var count = 0
    res.run {
        for (v in this@averageTo) {
            x += v.x
            y += v.y
            z += v.z
            w += v.w
            count++
        }
        res.set(x/count, y/count, z / count, w / count)
    }
}
